<template>
  <h1 style="margin-bottom: 30px ;">学员信息统计</h1>
  <div class="chart-wrapper">
    <div class="chart-container1">
      <!-- ECharts 容器 -->
      <div class="chart" id="container1" />
    </div>

    <div class="chart-container2">
      <!-- {{ studentDegreeData }} -->
      <!-- ECharts 容器 -->
      <div class="chart" id="container2" />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'

import { selectStudentDegreeDataApi } from '../../../api/report/studentDegreeData'
import { selectStudentCountDataApi } from '../../../api/report/studentCountData'
import * as echarts from 'echarts'

let studentDegreeData = ref([])

// 存储图表实例
let myChart = null
let myChart2 = null
// 获取班级人数报表数据
async function selectStudentConutData() {
  const result = await selectStudentCountDataApi()
  if (result.code == 1) {
    let clazzList = result.data.clazzList
    console.log(clazzList)
    let dataList = result.data.dataList

    initChart1(clazzList, dataList)
  } else {
    ElMessage.error(result.message)
  }
}

// 获取学员学历报表数据
async function selectStudentDegreeData() {
  const result = await selectStudentDegreeDataApi()
  if (result.code == 1) {
    result.data.forEach(DegreeData => {
      studentDegreeData.value.push(DegreeData)
    });

    initChart2()
  } else {
    ElMessage.error(result.message)
  }
}



function initChart1(clazzList, dataList) {

  // 初始化图表实例
  myChart = echarts.init(document.getElementById('container1'))

  // 图表配置项
  const option = {
    tooltip: {},
    xAxis: {
      type: 'category',
      data: clazzList,
      axisLabel: {
        fontSize: 12, // 设置 x 轴文字大小，可根据需要调整
      },
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: dataList,
        type: 'bar',
        itemStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 1,
            y2: 0,
            colorStops: [
              { offset: 0, color: '#fa709a' },   // 粉红色
              { offset: 1, color: '#fee140' }    // 浅黄色
            ]
          }
        }
      }
    ]
  }
  // 设置图表配置
  myChart.setOption(option)
}

function initChart2() {
  // 初始化图表实例
  myChart2 = echarts.init(document.getElementById('container2'))

  // 图表配置项
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: '5%',
      left: 'center'
    },
    series: [
      {
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        padAngle: 5,
        itemStyle: {
          borderRadius: 10
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 20,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: studentDegreeData.value
      }
    ]
  };
  // 设置图表配置
  myChart2.setOption(option)
}









// 在组件挂载后初始化图表
onMounted(() => {
  selectStudentConutData()
  selectStudentDegreeData()
  if (myChart) {
    myChart.dispose()
    myChart = null
  }
  if (myChart2) {
    myChart2.dispose()
    myChart2 = null
  }
})
</script>

<style scoped>
.chart-container1 {
  width: 70%;
  height: 600px;
  /* 图表容器高度 */
}

.chart-container2 {
  width: 30%;
  height: 600px;
  /* 图表容器高度 */
}

.chart {
  width: 100%;
  height: 100%;
}

.chart-wrapper {
  display: flex;
  flex-direction: row;
  gap: 0px; /* 两个图表之间的间距 */
}
</style>
